package com.sc.controller.patrol;

import com.sc.domain.patrol.PatrolPlan;
import com.sc.dto.PatrolPlanDTO;
import com.sc.result.PageResult;
import com.sc.result.Result;
import com.sc.service.patrol.PatrolPlanService;
import com.sc.vo.PatrolPlanVO;
import lombok.RequiredArgsConstructor;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 巡检计划管理
 * @author: Zuiliu
 * 功能：增删改查、作列表展示。
 * 数据格式
 */
@CrossOrigin
@RestController
@RequestMapping("/patrolPlan")
@RequiredArgsConstructor
public class PatrolPlanController {

    private final PatrolPlanService ivrs;

    //增删改查、作列表展示
    //添加巡检计划
    @PostMapping("/addPatrolPlan")
    public Result addPatrolPlan(@RequestBody PatrolPlan patrolPlan) {
        try {
            return Result.success("添加成功", ivrs.save(patrolPlan));
        } catch (DuplicateKeyException e) {
            return Result.error("巡更计划名称重复，换一个试试");
        }
    }

    //删除巡检计划
    @DeleteMapping("/deletePatrolPlan")
    public Result deletePatrolPlan(Integer id) {
        return Result.success("删除成功", ivrs.removeById(id));
    }

    @DeleteMapping("/deleteByIds")
    public Result deleteByIds(@RequestBody List<Integer> ids) {
        return Result.success("删除成功", ivrs.removeByIds(ids));
    }

    // 修改巡检计划
    @PutMapping("/updatePatrolPlan")
    public Result updatePatrolPlan(@RequestBody PatrolPlan patrolPlan) {
        try {
            return Result.success("修改成功", ivrs.updateById(patrolPlan));
        } catch (DuplicateKeyException e) {
            return Result.error("巡更计划名称重复，换一个试试");
        }
    }

    //查询巡检计划
    @GetMapping("/selectPatrolPlan")
    public Result selectPatrolPlan(Integer id) {
        return Result.success(ivrs.getById(id));
    }

    @PostMapping("/listPatrolPlan")
    public Result<PageResult<PatrolPlanVO>> listPatrolPlan(@RequestBody PatrolPlanDTO patrolPlanDTO) {
        return Result.success(ivrs.pageQuery(patrolPlanDTO));
    }

    // 查询所有记录的方法
    @GetMapping("/getAll")
    public Result<List<PatrolPlan>> getAll() {
        return Result.success(ivrs.list());
    }
}
